/*
 *  Copyright (c) 2009, Hugo Augusto Alves
 *  All rights reserved.
 * 
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *        notice, this list of conditions and the following disclaimer in the
 *        documentation and/or other materials provided with the distribution.
 *      * Neither the name of the Hugo Augusto Alves nor the
 *        names of its contributors may be used to endorse or promote products
 *        derived from this software without specific prior written permission.
 * 
 *  THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
 *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.wiscriptor.wiki;

import java.io.File;
import org.wiscriptor.core.WisProperties;
import org.wiscriptor.io.FileHandler;
import org.wiscriptor.io.FileLoader;
import org.wiscriptor.utils.Utils;

/**
 *
 * @author Hugo
 */
public class Wiki {

    private String wikiName = "";
    private FileHandler wikiHandler;
    private FileHandler pagesHandler;
    private FileHandler historyHandler;
    private FileHandler imagesHandler;
    private boolean created = false;

    public Wiki(String wikiName) {
        this.wikiName = wikiName;
        File baseDir = null;
        File wikiDir = null;
        File pagesDir = null;
        File historyDir = null;
        File imagesDir = null;
        try {
            baseDir = FileLoader.getInstance().loadFile(WisProperties.wikisDir);
            baseDir.mkdir();
            wikiDir = new File(baseDir, wikiName);
            wikiDir.mkdir();
            pagesDir = new File(wikiDir, WisProperties.pagesDir);
            pagesDir.mkdir();
            historyDir = new File(wikiDir, WisProperties.historyDir);
            historyDir.mkdir();
            imagesDir = new File(wikiDir, WisProperties.imagesDir);
            imagesDir.mkdir();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        this.wikiHandler = new FileHandler(wikiDir);
        this.pagesHandler = new FileHandler(pagesDir);
        this.historyHandler = new FileHandler(historyDir);
        this.imagesHandler = new FileHandler(imagesDir);

        if (baseDir != null && baseDir.exists() &&
                wikiDir != null && wikiDir.exists() &&
                pagesDir != null && pagesDir.exists() &&
                historyDir != null && historyDir.exists() &&
                imagesDir != null && imagesDir.exists()) {
            created = true;
        } else {
            created = false;
        }
    }

    public boolean isCreated() {
        return created;
    }

    public String getName() {
        return wikiName;
    }

    public String getDbName() {
        String dbPath = WisProperties.wikisDir;
        try {
            dbPath = (FileLoader.getInstance().loadFile(WisProperties.wikisDir)).getAbsolutePath();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return dbPath + File.separator + wikiName + File.separator + wikiName + WisProperties.wikidbExt;
    }

    private Utils.Status savePage(String filename, String text) {
        if (!created) {
            return Utils.Status.ERROR;
        }
        try {
            pagesHandler.save(filename + WisProperties.pageExt, text);
            return Utils.Status.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Utils.Status.ERROR;
        }
    }

    public Utils.Status savePage(Page page) {
        if (!created) {
            return Utils.Status.ERROR;
        }
        try {
            pagesHandler.save(page.getName() + WisProperties.pageExt, page.getText());
            historyHandler.save(page.getName() + " v" + page.getVersion().getVersionString() + WisProperties.pageExt, page.getText());
            return Utils.Status.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Utils.Status.ERROR;
        }
    }

}
